package com.gw.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Map;

/**
 *  ll: 经纬度
 * @author King
 * @main 346238763@qq.com
 * @date 2018年3月20日
 */
public class Location  {
	public static class Pot{
		public double latitude;
		public double longtitude;
	}
	
	public static void  calculate (String path,String out) throws IOException {
		Map<String,Map<String,Pot>> mm = new HashMap<String,Map<String,Pot>>();
		InputStream is= new FileInputStream(new File(path));
		BufferedReader br=new BufferedReader(new InputStreamReader(is));
		String s=null;
		Map<String,Pot> mt = null;
		
		while((s=br.readLine())!=null){
			//			System.out.println(s);
			if (s.indexOf("省")!=-1) {
				String[] colums = s.split("：");
				if (colums.length < 2) {
					throw new IOException("geo 文件格式错误！");
				}
				mt = mm.get(colums[1]);
				if (mt == null) {
					mt = new HashMap<String,Pot>();
					mm.put(colums[1],mt);
				}
				continue;
			}
			if (mt == null) {
				throw new IOException("geo 文件格式有误!");
			}
			String[] colums = s.split(" ");
			if (colums.length < 3) {
				throw new IOException("geo 文件格式有误!");
			}
			String[] ic = colums[0].split(":");
			if (ic.length < 2) {
				throw new IOException("geo 文件格式有误!");
			}
			String city = ic[1];
			ic = colums[1].split(":");
			if (ic.length < 2) {
				throw new IOException("geo 文件格式有误!");
			}
			double longtitude = Double.valueOf(ic[1]).doubleValue();
			ic = colums[2].split(":");
			if (ic.length < 2) {
				throw new IOException("geo 文件格式有误!");
			}
			
			double latitude = Double.valueOf(ic[1]).doubleValue();
			Pot l = new Pot();
			l.longtitude = longtitude;
			l.latitude = latitude;
			mt.put(city,l);
		}
		OutputStream os= new FileOutputStream(new File(out));
		BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(os));
		
		for (int i = 731; i < 1360; i++) {
			for (int j = 31; j < 540; j++) {
				double longtitude = (2*i-1)/20.0;
				double latitude = (2*j-1)/20.0;
				double dist = 0;
				String region = null;
				for (String province : mm.keySet()) {
					Map<String,Pot> im = mm.get(province);
					for (String scity : im.keySet()) {
						Pot p = im.get(scity);
						

						double l = (p.latitude - latitude)*(p.latitude - latitude)+(p.longtitude - longtitude)*(p.longtitude - longtitude);
						//						System.out.println("longtitude:"+longtitude+"latitude:"+latitude+" l:"+l);

						if (region == null||l < dist) {
							region = province;
							dist = l;
						}
					}
				}
				String sla=String.format("%1$.1f", (j-1)/10.0);
				String slo=String.format("%1$.1f", (i-1)/10.0);

				String k = slo+"*"+sla;
				bw.write(k+","+region);
				bw.newLine();
			}
			
		}
		
	}
	public Map<String,String> m_geo = new HashMap<String,String>();
	
	public Location (String llPath) throws IOException{
		InputStream is=Location.class.getClassLoader().getResourceAsStream(llPath);	
		BufferedReader br=new BufferedReader(new InputStreamReader(is));
		String s=null;
		
		while((s=br.readLine())!=null){
			//			System.out.println(s);
			String[] colums = s.split(",");
			if (colums.length < 2) {
				throw new IOException("文件格式不正确!");
			}
			m_geo.put(colums[0],colums[1]);
		}
	}
	public  String getCNProvince (String longtitue,String latitude) {
		double la = Double.valueOf(latitude).doubleValue();
		double lo = Double.valueOf(longtitue).doubleValue();
		
		return getCNProvince(lo,la);
	}

	public  String getCNProvince (double longtitue,double latitude) {
		String sla = String.format("%1$.1f", Math.floor(latitude*10)/10);
		String slong = String.format("%1$.1f", Math.floor(longtitue*10)/10);
		String key = slong+"*"+sla;
		//		System.out.println("find key:"+key);
		

		return m_geo.get(key);
	}

	public static void main(String[] args)throws Exception {
//		calculate(args[0],args[1]);
		String longtitude = "115";
		String latitude = "28";
		String filePath = "geo.csv";
		Location l = new Location(filePath);

		System.out.println("经度:"+longtitude+" 纬度:"+latitude+" 省份:"+l.getCNProvince(longtitude,latitude));

	}

}
